import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'
import http from '../plugins/http'
import Index from '../views/Index.vue'
import UserList from  '../views/UserList.vue'
import userInsert from '../views/list/UserInsert.vue'
import userEdit from '../views/list/UserEdit.vue'
import userpermission from '../views/list/Permission.vue'
import productList from '../views/product/ProductList.vue'
import productEdit from '../views/product/ProductEdit.vue'
import productInsert from '../views/product/ProductInsert.vue'

import brandList from '../views/brand/BrandList.vue'
import brandEdit from '../views/brand/BrandEdit.vue'
import brandInsert from '../views/brand/BrandInsert.vue'

import cateList from '../views/cate/CateList.vue'
import cateEdit from '../views/cate/CateEdit.vue'
import cateInsert from '../views/cate/CateInsert.vue'

import roleList from '../views/role/RoleList.vue'
import roleEdit from '../views/role/RoleEdit.vue'
import roleInsert from '../views/role/RoleInsert.vue'
import rolepermission from '../views/role/RolePermission.vue'

import datasale from '../views/data/DataSale.vue'
Vue.use(VueRouter)

  const routes = [
  {
    path: '/',
    name: 'Index',
    redirect:'/user/list',
    component: Index,
    children:[
      {
        path: 'user/list',
        name: 'UserList',
        component: UserList,
      },
     
      {
        path:'userInsert',
        name:'userInsert',
        component:userInsert
      },
      {
        path:'userEdit/:id',
        name:'userEdit',
        component:userEdit
      },
      {
        path:'user/permission/:id',
        name:'userpermission',
        component:userpermission
      }, 
      {
        path:'product/list',
        name:'productList',
        component:productList
      },
      {
        path:'productInsert',
        name:'productInsert',
        component:productInsert
      },
      {
        path:'productEdit/:id',
        name:'productEdit',
        component:productEdit
      },
      {
        path:'brand/list',
        name:'brandList',
        component:brandList
      },
      {
        path:'brandInsert',
        name:'brandInsert',
        component:brandInsert
      },
      {
        path:'brandEdit/:id',
        name:'brandEdit',
        component:brandEdit
      },
      {
        path:'cate/list',
        name:'cateList',
        component:cateList
      },
      {
        path:'cateInsert',
        name:'cateInsert',
        component:cateInsert
      },
      {
        path:'cateEdit/:id',
        name:'cateEdit',
        component:cateEdit
      },
      {
        path:'role/list',
        name:'roleList',
        component:roleList
      },
      {
        path:'roleInsert',
        name:'roleInsert',
        component:roleInsert
      },
      {
        path:'roleEdit/:id',
        name:'roleEdit',
        component:roleEdit
      },
      {
        path:'role/permission/:id',
        name:'rolepermission',
        component:rolepermission
      }, 
      {
        path:'data/sale',
        name:'datasale',
        component:datasale
      },
     
    ]
  },
  {
    path: '/login',
    component: () => import('../views/Login.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

let whiteList = ['/login', '/2', '/3']
router.beforeEach(async (to, from, next) => {
  if(whiteList.includes(to.path)){
    next()
  }else{
    if(store.state.user.id){
      next()
    }else{
      let res = await http.get('/user/info')
      if(res.errcode == 0){
        store.commit('setUser', res.data)
        next()
      }else{
        console.log(4)
        router.push('/login')
      }
    }
  }
})

export default router
